package com.dongdongshop.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.dongdongshop.page.PageResult;
import com.dongdongshop.service.EsService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.lucene.search.TotalHits;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@Service
@DubboService
public class EsServiceImpl implements EsService {
    @Autowired
    private RestHighLevelClient restHighLevelClient;
    @Override
    public PageResult getItemList(String title, Integer pageNum, Integer pageSize) throws IOException {
        //创建一个搜索
        SearchRequest searchRequest = new SearchRequest("tbitem");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("title",title);
        searchSourceBuilder.from(pageNum);
        searchSourceBuilder.size(pageSize);
        searchSourceBuilder.query(matchQueryBuilder);

        //创建高亮显示对象
        HighlightBuilder highlightBuilder = new HighlightBuilder();
        //设置高亮前缀
        highlightBuilder.preTags("<span style='color:red'>");
        //设置高亮字段
        highlightBuilder.field("title");
        //设置高亮后缀
        highlightBuilder.postTags("</span>");
        searchSourceBuilder.highlighter(highlightBuilder);
        searchRequest.source(searchSourceBuilder);
        SearchResponse response = response = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
        SearchHits hits = response.getHits();
        TotalHits totalHits = hits.getTotalHits();
        String s = JSONObject.toJSONString(totalHits);
        JSONObject jsonObject = JSONObject.parseObject(s);
        Object value = jsonObject.get("value");
        Long total = Long.valueOf(value.toString());
        Long pages = total % pageSize == 0 ? total / pageSize : total / pageSize + 1;
        SearchHit[] hits1 = hits.getHits();
        List<Map<String,Object>> list = new ArrayList<>();
        for (SearchHit documentFields : hits1) {
            Map<String, HighlightField> highlightFields = documentFields.getHighlightFields();
            HighlightField title2 = highlightFields.get("title");
            Map<String, Object> documentFields1 = documentFields.getSourceAsMap();
            if (title != null){
                Text[] fragments = title2.fragments();
                String name = "";
                for (Text text : fragments) {
                    name += text;
                }
                documentFields1.put("title",name);
            }
            list.add(documentFields1);
        }
        return new PageResult(pageNum,pageSize, total, Math.toIntExact(pages),list);
    }
}
